---
title: 'Replication materials for It’s Trade, Stupid! How Changes in Trade Competitiveness Affect Incumbents’ Electoral Success'
author: Andreas Dür, Robert A. Huber, and Yannick Stiller
output:
  rmdformats::downcute:
    use_bookdown: true
    self_contained: true
    thumbnails: false
    lightbox: true
    gallery: false
    highlight: monochrome
  editor_options:
  chunk_output_type: console
---

```{r globopt, include = F}
knitr::opts_chunk$set(warning = FALSE)
```


# How to proceed

All steps required to replicate the results in `R` can be run from the file `master_replication.R`. This file loads various individual scripts which recode and clean the data, provide descriptive evidence and run the analyses.

# Prepare Replication Data

After downloading our replication material from <https://doi.org/10.7910/DVN/OCCX2K>, please open the file `replication_material.Rproj`. You need to have installed `R` (version 4.2x) and `RStudio` for this. After opening the `.Rproj` file in `RStudio`, please open the file `master_replication.R` within the project. This script can be found in the subfolder `r_code`. In the next steps, we describe each of the steps of the `master_replication.R` file.

# Install/Load Packages

The following code installs all packages (if required). After that it loads the packages. The code then also uses the `here` package to set the working directory. Runtime of this code depends on the number of packages that you have already installed. However, even when you have no packages installed, it should only take a few minutes.

```{r pkgload}
run_start_total <- Sys.time()
run_start <- Sys.time()

pkgs <- c("tidyverse",
          "marginaleffects")

# Function to check if packages are installed
# If not: package will be installed from CRAN and then loaded
# If: Package will be loaed

install_load <- function(packages){
  
  for (p in packages) {
    cat("Check package: '", p, "'...\n", sep = "")
    flush.console()
    
    if (p %in% rownames(installed.packages())) {
      
      cat("Package: '", p, "' is already installed...\n\n", sep = "")
      flush.console()
      
      library(p, character.only=TRUE)
      
    } else {
      
      cat("Package: '", p, "' is NOT installed! Will install now...\n\n", sep = "")
      install.packages(p)
      library(p,character.only = TRUE)
      
    }
  }
  cat("\nAll packages installed!\n\n")
}

# Apply function to all required packages

install_load(pkgs)

# Set wd with here() package

here::i_am("replication_material.Rproj")

run_stop <- Sys.time()
run_time <- (run_stop - run_start)
run_time
```

# Recoding core variable

The script `recode` in the subfolder `r_code` loads the underlying data, and cleans this data. 

```{r, include=T}
run_start <- Sys.time()

source("r_code/recode.R", echo = T)

run_stop <- Sys.time()
run_time <- (run_stop - run_start)
run_time
```

# Descriptive statistics

The script `descriptives.R` in the subfolder `r_code` creates `TableA1.tex` in the `output` folder. 

```{r, include=T}
run_start <- Sys.time()

source("r_code/descriptives.R", echo = T)

run_stop <- Sys.time()
run_time <- (run_stop - run_start)
run_time
```

# Analysis

This script `analysis.R` in the subfolder `r_code` runs all analyses shown in the paper and the appendix.

```{r, include=T}
run_start <- Sys.time()

source("r_code/analysis.R", echo = T)

run_stop <- Sys.time()
run_time <- (run_stop - run_start)
run_time
```

# Session Info

This notebook was run using the following setup:

```{r}
pander::pander(sessionInfo())

run_stop_total <- Sys.time()
run_time_total <- (run_stop_total - run_start_total)
run_time_total
```
